﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using Entidades;
using System.ComponentModel;


namespace LNIUS
{
   public class CtrTerceros : CtrBase
    {
        iusEntities ctx;
        terceros aux;

        public terceros Aux
        {
            get { return aux; }
            set { aux = value; }
        }

        public CtrTerceros(){
            aux=(terceros)this.Entidad;
        }

        public string GuardarNuevo(terceros ent)
        {
            try
            {
                using ( ctx = new iusEntities())
                {
                    ctx.AddToterceros(ent);
                    ctx.SaveChanges();
                    this.Msg = "Se registró el tercero con éxito";
                    this.LError = false;
                    return this.Msg;
                }
            }
            catch (Exception ex)
            {
                if (ex.InnerException.Message != null)
                {
                    this.Msg = ex.InnerException.Message;
                }
                else
                {
                    this.Msg = ex.Message;
                }
                this.LError = true;
                return this.Msg;
            }
        }

       
        public List<terceros> FindList(string filtro)
        {
            using ( ctx = new iusEntities())
            {
                var q = from c in ctx.terceros
              //          where c.NOMBRE.ToUpper().Contains(filtro.ToUpper())
                        select c;
                return q.ToList();
            }
        }

        public static string FindTercero(string nombre)
        {
            iusEntities ctx;
            terceros aux = new terceros();
            using (ctx = new iusEntities())
            {
                aux = (from c in ctx.terceros
                       where c.NOMBRES.ToUpper().Equals(nombre.ToUpper())
                       select c).FirstOrDefault();
                return aux.ID;
            }
        }

        public static string FindNomTercero(string id)
        {
            iusEntities ctx;
            terceros aux = new terceros();
            using (ctx = new iusEntities())
            {
                aux = (from c in ctx.terceros
                       where c.ID.Equals(id)
                       select c).FirstOrDefault();
                if (aux != null)
                {
                    return aux.NOMBRES + " " + aux.APELLIDOS;
                }
                else
                {
                    return "";
                }
            }
        }

       [DataObjectMethod(DataObjectMethodType.Select, true)]
       public List<terceros> FindAll()
       {
           using (ctx = new iusEntities())
           {
                   var q = from u in ctx.terceros
                           //where ud.id_dep == dependencia &&
                           //!(from un in ctx.not_terceros where un.NOTICIAS_ID == IdNot select un.terceros_ID).Contains(u.ID)
                           select u;
                   return q.ToList();
               }
       }

       [DataObjectMethod(DataObjectMethodType.Select, true)]
       public List<terceros> FindAll(string filtro)
       {
           using (ctx = new iusEntities())
           {
               if (filtro != null)
               {
                   double num;
                   if (double.TryParse(filtro, out num))
                   {
                       var q = from u in ctx.terceros
                               where u.ID.Contains(filtro) || u.NOMBRES.Contains(filtro)
                               //!(from un in ctx.not_terceros where un.NOTICIAS_ID == IdNot select un.terceros_ID).Contains(u.ID)
                               select u;
                       return q.ToList();
                   }
                   else
                   {
                       var q = from u in ctx.terceros
                               where u.NOMBRES.Contains(filtro)
                               //!(from un in ctx.not_terceros where un.NOTICIAS_ID == IdNot select un.terceros_ID).Contains(u.ID)
                               select u;
                       return q.ToList();
                   }
               }
               else
               {
                   var q = from u in ctx.terceros
                           //where u.NOMBRE.Contains(filtro)
                           //!(from un in ctx.not_terceros where un.NOTICIAS_ID == IdNot select un.terceros_ID).Contains(u.ID)
                           select u;
                   return q.ToList();
               }
           }
       }

        public bool FindKey(string Id) {
            using (ctx = new iusEntities())
            {
                ctx.ContextOptions.LazyLoadingEnabled = false;
                aux=pFindKey(Id);
                if (!(aux == null))
                {
                    ctx.Detach(aux);//saca del contexto
                    return true;
                }
                else
                {
                    return false;
                }
 
            }
        }

        private terceros pFindKey(string Id)
        {
                aux= (from s in ctx.terceros
                       where s.ID == Id
                       select s).FirstOrDefault();
                
                return aux;
        }

      
        public string GuardarModif(terceros ent,string Id){
            try
            {
                using (ctx = new iusEntities()){
                   aux=pFindKey(Id);
                   if (!(ent==null)){
                        aux.ID = ent.ID;
                        aux.NOMBRES= ent.NOMBRES;
                        aux.TIP_DOC = ent.TIP_DOC;
                        aux.SEXO = ent.SEXO;
                        aux.DPTO_RES = ent.DPTO_RES;
                        aux.MCPIO_RES = ent.MCPIO_RES;
                        aux.APELLIDOS = ent.APELLIDOS;
                        aux.ESTADO_CIVIL = ent.ESTADO_CIVIL;
                        aux.LUGAR_EXP = ent.LUGAR_EXP;
                        aux.REP_LEGAL = ent.REP_LEGAL;
                        aux.ID_REP_LEGAL = ent.ID_REP_LEGAL;
                        aux.CELULAR = ent.CELULAR;
                        aux.TELEFONO=ent.TELEFONO;
                        aux.EMAIL=ent.EMAIL;
                        aux.DIRECCION=ent.DIRECCION;
                        if (ent.FOTO != null)
                        {
                            aux.FOTO = ent.FOTO;
                        }
                        aux.USAP = ent.USAP;
                        aux.FEC_REG = DateTime.Now;
                        aux.FEC_NAC = ent.FEC_NAC;
                        int num = ctx.SaveChanges();
                        this.Msg = "Se modificó el tercero con éxito.";
                        this.LError = false;
                        return this.Msg;
                    }
                }
                return "nada";
            }
            catch (Exception ex)
            {
                this.Msg = ex.InnerException.Message;
                this.LError = true;
                return this.Msg;
            }
        }

        public string Eliminar(string Id) {
            try
            {
                using (ctx = new iusEntities())
                {
                     aux = pFindKey(Id);
                    if (!(aux == null))
                    {
                        ctx.DeleteObject(aux);
                        int num = ctx.SaveChanges();
                        this.Msg = "Se eliminó el tercero con éxito.";
                        return this.Msg;
                    }
                }
                return "El registro no se encuentra";
            }
            catch (Exception ex)
            {
                this.Msg = ex.InnerException.Message;
                return this.Msg;
            }
        
        }

       [DataObjectMethod(DataObjectMethodType.Select, true)]
        public IList<terceros> Find(string id, string nombre, string SortColumns, int startRow, int maxRows)
        {
            using (ctx = new iusEntities())
            {
                var query = from p in ctx.terceros select p;

                query = FindFilter(query, id, nombre);
                query = FindSort(query, SortColumns);

                return query.Skip(startRow).Take(maxRows).ToList();
            }
        }

       [DataObjectMethod(DataObjectMethodType.Select, true)]
       public IList<terceros> Find(string SortColumns, int startRow, int maxRows)
       {
           using (ctx = new iusEntities())
           {
               var query = from p in ctx.terceros select p;

               //query = FindFilter(query, id, nombre);
               query = FindSort(query, SortColumns);

               return query.Skip(startRow).Take(maxRows).ToList();
           }
       }

        private static IQueryable<terceros> FindFilter(IQueryable<terceros> query, string id, string nombre)
        {
            if (!string.IsNullOrEmpty(id))
                query = query.Where(p => p.ID == id);

            if (!string.IsNullOrEmpty(nombre))
                query = query.Where(p => p.NOMBRES.Contains(nombre));

            return query;
        }

        private IQueryable<terceros> FindSort(IQueryable<terceros> query, string sortExpression)
        {
            if (string.IsNullOrEmpty(sortExpression))
             return query.OrderBy(p => p.NOMBRES);
            var parameter = Expression.Parameter(typeof(terceros), "terceros");
               
            if (sortExpression.IndexOf(" DESC") < 0)
            {
                return query.OrderBy(Expression.Lambda<Func<terceros, object>>(Expression.Property(parameter, sortExpression), parameter));
            }
            else {
                sortExpression = sortExpression.Replace(" DESC", "");
                return query.OrderByDescending(Expression.Lambda<Func<terceros, object>>(Expression.Property(parameter, sortExpression), parameter));
            }
        }

        public int FindCount(string id, string nombre)
        {
            using (ctx = new iusEntities())
            {
                var query = from p in ctx.terceros select p;

                query = FindFilter(query, id, nombre);

                return query.Count();
            }
        }

        public int FindCount()
        {
            using (ctx = new iusEntities())
            {
                var query = from p in ctx.terceros select p;

               // query = FindFilter(query);

                return query.Count();
            }
        }
        
       public  List<Nomterceros> List()
        {
            using (ctx = new iusEntities())
            {
                var q = from c in ctx.terceros
                        //          where c.NOMBRE.ToUpper().Contains(filtro.ToUpper())
                        select(new Nomterceros{Nombre=c.NOMBRES});
                return q.ToList();
            }
        }
     

    }
   public class Nomterceros
   {
       public string Nombre { get; set; }
   }
    }
